package com.dyzx.hbb.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseCustomController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.sys.entity.SysLogOperation;
import com.dyzx.hbb.modules.sys.entity.SysOrder;
import com.dyzx.hbb.modules.sys.query.LogOperationQuery;
import com.dyzx.hbb.modules.sys.service.SysLogOperationService;

import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/sys/log/operation")
@RestController
@Tag(name = "11.系统日志", description = "系统日志相关接口")
public class LogOperationController extends BaseCustomController<SysLogOperationService, SysLogOperation, LogOperationQuery> {

    @Autowired
    private SysLogOperationService service;


    @Override
    protected SysLogOperationService getBaseService() {
        return service;
    }


    @Override
    protected MyLambdaQueryWrapper<SysLogOperation> queryWrapper(LogOperationQuery query) {
        MyLambdaQueryWrapper<SysLogOperation> wrapper = new MyLambdaQueryWrapper<>();
       wrapper.select(SysLogOperation::getId, SysLogOperation::getType,SysLogOperation::getUrl,SysLogOperation::getMode,
               SysLogOperation::getData,
               SysLogOperation::getModel, SysLogOperation::getAccount,  SysLogOperation::getIp,SysLogOperation::getTerminal,SysLogOperation::getCreateDate).userName(SysLogOperation::getUserId).as("userName");

        if (query != null) {

           wrapper.like(StringUtils.isNotBlank(query.getAccount()), SysLogOperation::getAccount, query.getAccount())
                   .inSet(StringUtils.isNotBlank(query.getAccount()), SysLogOperation::getAccount, query.getAccount())
                   .inSet(query.getType() != null, SysLogOperation::getType, query.getType())
                   .inSet(query.getMode() != null, SysLogOperation::getMode, query.getMode())
                   .inSet(query.getIp() != null, SysLogOperation::getIp, query.getIp())
                   .dateBetween(StringUtils.isNotBlank(query.getCreateDate()), SysLogOperation::getCreateDate, DateFormat.DATE, query.getCreateDate())
                  .inSet(query.getTerminal()!= null, SysLogOperation::getTerminal, query.getTerminal());

        }
        return wrapper;

    }



    @Override
    protected MyLambdaQueryWrapper<SysLogOperation> queryInfoWrapper() {
        MyLambdaQueryWrapper<SysLogOperation> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(SysLogOperation::getId, SysLogOperation::getType,SysLogOperation::getUrl,SysLogOperation::getData,
                SysLogOperation::getModel, SysLogOperation::getAccount,  SysLogOperation::getIp,SysLogOperation::getTerminal,SysLogOperation::getCreateDate).userName(SysLogOperation::getUserId).as("userName");

        return wrapper;
    }



    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        Long organId = SecurityContextHolder.getOrganId();
        QueryWrapper<SysLogOperation> queryWrapper = new QueryWrapper<>();
        SysLogOperation byId = getBaseService().getOne(queryWrapper.eq("id", id).eq("organ_id",organId).last("limit 1"));
        if (byId == null) {
            return Result.error("数据不存在");
        }

        boolean data = getBaseService().removeById(id);
        return data? Result.ok() :Result.error("删除失败");
    }



}
